package ca.abubakrsagar.breadboardsim;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JComponent;

public class BreadBoardPanelController implements MouseListener {
	private BreadboardPanel breadboardPanel; // The breadboard panel.
	private PropertiesPanel propertiesPanel; // The properties panel.

	/**
	 * Creates a new breadboard panel controller and associates the local panels
	 * with the given panels.
	 * 
	 * @param breadboardPanel
	 *            The specified breadboard panel.
	 * @param propertiesPanel
	 *            The specified properties panel.
	 */
	public BreadBoardPanelController(BreadboardPanel breadboardPanel,
			PropertiesPanel propertiesPanel) {
		this.breadboardPanel = breadboardPanel;
		this.propertiesPanel = propertiesPanel;

	}

	/**
	 * When the mouse is clicked, loop through all components to locate the
	 * component that is clicked. Once this component has been located, update
	 * the properties panel to associate with this specific component.
	 */
	@Override
	public void mouseClicked(MouseEvent e) {
		boolean found = false;
		for (int i = 0; i < breadboardPanel.getBreadboard().getLED().size(); i++) {
			if (e.getSource() == breadboardPanel.getBreadboard().getLED()
					.get(i)) {
				propertiesPanel.setPosition(i);
				propertiesPanel.setType(1);
				propertiesPanel.display();
				break;
			}
		}
		if (!found) {
			for (int i = 0; i < breadboardPanel.getBreadboard().getResistor()
					.size(); i++) {
				if (e.getSource() == breadboardPanel.getBreadboard()
						.getResistor().get(i)) {
					System.out.println(i);
					propertiesPanel.setPosition(i);
					propertiesPanel.setType(2);
					propertiesPanel.display();
					found = true;
					break;
				}
			}
		}

		if (!found) {
			for (int i = 0; i < breadboardPanel.getBreadboard().getSwitch()
					.size(); i++) {
				if (e.getSource() == breadboardPanel.getBreadboard()
						.getSwitch().get(i)) {
					propertiesPanel.setPosition(i);
					propertiesPanel.setType(3);
					propertiesPanel.display();
					found = true;
					break;
				}
			}
		}
		if (!found) {
			for (int i = 0; i < breadboardPanel.getBreadboard().getWire()
					.size(); i++) {
				if (e.getSource() == breadboardPanel.getBreadboard().getWire()
						.get(i)) {
					propertiesPanel.setPosition(i);
					propertiesPanel.setType(4);
					propertiesPanel.display();
					found = true;
					break;
				}
			}
		}
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {

	}

	@Override
	public void mouseExited(MouseEvent arg0) {

	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}
}
